<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;
use app\common\model\QuestionType;
use think\Db;
class Integral extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
    }
    /**
     * 扣分列表
     */
    public function index()
    {
        $param = $this->request->param();
        $where = [];
        if(isset($param['personnel_id'])){
            if (isset($param['fraction'])) {
                $where['fraction'] = $param['fraction'];
            }
            if (isset($param['reason'])) {
                $where['reason'] =  ['like', "%" . $param['reason'] . "%"];
            }
            $where['user'] = $param['personnel_id'];
            $page = input('page')?:0;//当前页数
            $list=model('integral')->where('status',['>',-1])->where($where)->paginate(config('page_number'));
            //获得总分
            $fraction=Db::name('integral')->where('status',['>',0])->where('user',$param['personnel_id'])->field('sum(fraction) as fraction')->select();
            $fraction=$fraction[0]['fraction'];
            return $this->fetch('index', ['integral' =>$list,'fraction'=>$fraction,'page'=>$page,'personnel_id'=>$param['personnel_id']]);
        }
        $this->error('请选择人才');
    }
    /**
     * 后台改变分数
     */
    public function add(){
        $param = $this->request->param();
        $user=$param['user'];
        if ($this->request->isPost()) {
            if($this->insert('integral', $param) === true){
                insert_admin_log('添加原因');
                $this->success('添加原因成功', url('admin/integral/index',['personnel_id' => $user]));
            }else{
                $this->error($this->errorMsg,url('admin/integral/index',['personnel_id' => $user]));
            }
        }
        return $this->fetch('addintegral',['personnel_id' => $user]);
    }
    /**
     * 编辑原因
     */
    public function edit()
    {
        $param = $this->request->param();
        $user=$param['user'];
        if ($this->request->isPost()) {
            if ($this->update('integral', $param, input('_verify', true)) === true) {
                insert_admin_log('修改了原因');
                $this->success('修改原因成功', url('admin/integral/index',['personnel_id' => $user]));
            } else {
                $this->error($this->errorMsg,['personnel_id' => $user]);
            }
        }
        return $this->fetch('addintegral', ['data' => model('integral')->where('id', input('id'))->find(),'user'=>$user]);
    }
    /**
     * 删除原因
     */
    public function del()
    {
        if ($this->request->isGet()) {
            $param = $this->request->param();
            $user=$param['user'];
            unset($param['user']);
            if ($this->delete('integral', $param ) === true) {
                insert_admin_log('删除了用户');
                $this->success('删除成功', url('admin/integral/index',['personnel_id' => $user]));
            } else {
                $this->error($this->errorMsg,['personnel_id' => $user]);
            }
        }
    }


    /**
     * 原因状态设置
     */
    public function teacherstatus(){
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if($this->update('integral', ['id'=>$param['id'],'status'=>$param['status']], input('_verify', true)) and $this->update('integral', ['id'=>$param['id'],'status'=>$param['status']], input('_verify', true),$field = true )){
               $this->success('操作成功！', url('admin/integral/index'));
            } else{
               $this->error($this->errorMsg);
            }
            $this->success('操作成功', url('admin/integral/index'));
        }
    }


}
